package com.tuananh.radnom.model.thresholding;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

import com.tuananh.radnom.model.netpoller.PollInfo;
import com.tuananh.radnom.model.notification.AbstractNotificator;
import com.tuananh.radnom.model.notification.Receiver;
import com.tuananh.radnom.model.userinfo.User;
import com.tuananh.radnom.model.userinfo.UserGroup;

@XmlDiscriminatorValue("default-thresholder")
public class DefaultThresholder extends AbstractThresholder {

	/**
	 * the default thresholder will handle the polling - collecting result which
	 * is string-based key-value pairs result
	 * 
	 */

	DataType dataType;
	CompareType compareType;

	@XmlAttribute(name = "data-type")
	@XmlJavaTypeAdapter(DataTypeXmlAdapter.class)
	public DataType getDataType() {
		return dataType;
	}

	public void setDataType(DataType dataType) {
		this.dataType = dataType;
	}

	@XmlAttribute(name = "compare-type")
	public CompareType getCompareType() {
		return compareType;
	}

	public void setCompareType(CompareType compareType) {
		this.compareType = compareType;
	}

	@Override
	public boolean compare(Object compareValue, PollInfo pollInfo,
			Object... args) {
		// TODO Auto-generated method stub
		// if datatype is not specified , use string as default
		if (dataType == null)
			dataType = new TypeSTRING();
		boolean result = false;
		HashMap<String, String> pairs = (HashMap<String, String>) compareValue;
		try {
			switch (compareType) {
			case EQUAL:
				result = dataType.isEqual(pairs.get(dsName), getValue());
				break;
			case NEQUAL:
				result = !dataType.isEqual(pairs.get(dsName), getValue());
				break;
			case LOWER:
				result = dataType.isLower(pairs.get(dsName), getValue());
				break;
			case HIGHER:
				result = dataType.isHigher(pairs.get(dsName), getValue());
				break;
			case CONTAIN:
				result = dataType.isContain(pairs.get(dsName), getValue());
				break;
			case NCONTAIN:
				result = !dataType.isContain(pairs.get(dsName), getValue());
				break;
			}
		} catch (Exception e) {
			result = false;
		}
		// generate an notification to send to network administrator
		if (!result) {
			currentFail++;
			if (currentFail >= this.toleranceTime) {
				for (AbstractNotificator notificator : this.notificator) {
					ArrayList<User> users = new ArrayList<User>();

					users = User.getUsersFromReceivers(notificator
							.getReceiver(),
							(args[0] != null) ? (ArrayList<User>) args[0]
									: null,
							(args[1] != null) ? (ArrayList<UserGroup>) args[1]
									: null);

					String data = "Thresholding value reached at netinterface "
							+ pollInfo.getNetInt().getName() + "\n IP = "
							+ pollInfo.getNetInt().getIpAddress()
							+ "\n NetService: "
							+ pollInfo.getNetService().getName() + "\n Time:"
							+ new Date().toString() + "\nDetail information\n"
							+ printDetail(pairs.get(this.dsName));

					notificator.notifyError(data, users);
				}
			}
		}

		return result;
	}

	public void sendNotification(PollInfo pollInfo, Object pollResult) {

	}

	public String printDetail(String extendInfo) {
		return " Ds-name:" + dsName + "   Data-type:" + dataType
				+ "   Compare-type:" + compareType + "\n Accepted-Value="
				+ value + "    Real-Value=" + extendInfo;
	}
}
